package com.huawei.petapp2.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.huawei.petapp2.R;
import com.huawei.petapp2.adapter.SearchGoodsAdapter;
import com.huawei.petapp2.entity.Storeitem;

import java.util.ArrayList;
import java.util.List;

public class GoodResultActivity extends AppCompatActivity {
    private Context mContext;
    List<Storeitem>storeitems=new ArrayList<>();;

    protected void onCreate(Bundle savedInstanceState) {

        //创建方法super.onCreate(),和布局加载方法setContentView()一定要放在最前面，不然可能会造成空指针现象
        super.onCreate(savedInstanceState);
        ArrayList<Storeitem> listgoods =  (ArrayList<Storeitem>) getIntent().getSerializableExtra("listobj");
        System.out.println("传输过来的"+listgoods);
        setContentView(R.layout.activity_good_result);
        initFrd(listgoods);
        //ViewGroup container=(LinearLayout)((ActivityGroup)getParent()).getWindow().findViewById(R.id.app_home_list);
       // onCreateView(getLayoutInflater(),container,savedInstanceState);
        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.app_home_list);
        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        SearchGoodsAdapter adapter = new SearchGoodsAdapter(getApplicationContext(),storeitems,savedInstanceState);
        recyclerView.setAdapter(adapter);


    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
//        return inflater.inflate(R.layout.tab02, container, false);
        View view = inflater.inflate(R.layout.activity_good_result, container, false);
        RecyclerView recyclerView = view.findViewById(R.id.app_home_list);
        ArrayList<Storeitem> listgoods =  (ArrayList<Storeitem>) getIntent().getSerializableExtra("listobj");
        initFrd(listgoods);
        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        SearchGoodsAdapter adapter = new SearchGoodsAdapter(getApplicationContext(),storeitems,savedInstanceState);
        recyclerView.setAdapter(adapter);


        return view;
    }

    private void initFrd(List<Storeitem>listgoods) {
        for(int i=0;i<listgoods.size();i++)
        {
           Storeitem storeitem=new Storeitem();
           storeitem.setItemImg(listgoods.get(i).getItemImg());
            storeitem.setItemPrice(listgoods.get(i).getItemPrice());
            storeitem.setItemDes(listgoods.get(i).getItemDes());
            storeitem.setItemName(listgoods.get(i).getItemName());
            storeitems.add(storeitem);
        }
    }
}